<?php

namespace app\admin\model;

use app\admin\utils\ontimeLog;
use think\Db;
use think\Exception;


class ProductModel extends Model
{
    //分页查询
    public function list($page, $limit, $param)
    {
        return Db::table('product')
            ->where($param)
            ->order('product.add_time', 'desc')
            ->page($page, $limit)
            ->select();
    }


    public function add($data)
    {
        Db::table('product')->insert($data);
    }

    public function update($id, $data)
    {
        Db::table('product')->where('id', $id)->update($data);
    }

    public function count(): int
    {
        return Db::table('product')->count();
    }

    public function delete($id): int
    {
        try {
            return Db::table('product')->where('id', $id)->delete();
        } catch (Exception $e) {
            return -1;
        }
    }

    public function uploadProductStatus($id, $status)
    {
        try {
            return Db::table('product')->update(['status' => $status, 'id' => $id]);
        } catch (Exception $e) {
            return -1;
        }
    }

    //查询分类列表
    public function listCate()
    {
        return Db::table('product_cates')->select();
    }

    public function addCate($data)
    {
        return Db::table('product_cates')->insert($data);
    }

    public function uploadCateStatus($id, $status)
    {
        try {
            return Db::table('product_cates')->update(['status' => $status, 'id' => $id]);
        } catch (Exception $e) {
            return -1;
        }
    }

    //增加SKU项
    public function addSkuData($data)
    {
        return Db::table('product_sku')->insert($data);
    }
}